<?php
if($_POST['submit_form']==true){
   //Inicialising the variables
   $tut_name = htmlspecialchars(trim($_POST['tut_name']));
   $tut_descr = htmlspecialchars(trim($_POST['tut_descr']));
   $date_added = time();
   $from_who = $_SESSION['username'];
   
   //Select the name of the tutorial
   $select_name = mysql_query('SELECT name FROM tutorials WHERE name = "'.$tut_name.'"');
   $count = mysql_num_rows($select_name);
   
   //If there is another tutorial with the same name, we stop the code
   if($count != 0){
      echo '<p class="error">Вече съществува такъв самоучител!</p>';
   } else {
   //Checking the variables - if they`re empty
   if(empty($tut_name) || empty($tut_descr)){
      echo '<p class="error">Моля, попълнете ВСИЧКИ полета!</p>';
   } else {
      //Checking the length of the variables
      if(strlen($tut_name) < 3) {
	     echo '<p class="error">Кратко име на самоучителя!</p>';
		 $error_name = true;
	  }
	  if(strlen($tut_descr) < 6) {
	     echo '<p class="error">Кратко описание на самоучителя!</p>';
		 $error_descr = true;
	  }
	  
	  //If everything is ok
	  if($error_name != true && $error_descr != true){
	     //Insert the information in the database
	     $insert_tut = mysql_query('INSERT INTO tutorials(name, descr, date_added, from_who)
		 VALUES("'.$tut_name.'", "'.$tut_descr.'", '.$date_added.', "'.$from_who.'")');
		 if($insert_tut) echo "<p class='success'>Успешен запис!</p>";
	  }
   }
   }
} else {
   echo '';
}
?>